package MySQL;

import back.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MySQL_User {
    //增
    public boolean addUser(User user){
        String sql = "INSERT INTO user (user_ip, user_password) VALUES (?, ?)";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, user.getId());
            pstmt.setString(2, user.getPassword());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch (SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    //改
    public boolean updateUser(User user){
        String sql = "UPDATE user SET user_password = ? WHERE user_ip = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, user.getPassword());
            pstmt.setString(2, user.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch (SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    //删
    public boolean deleteUser(User user){
        String sql = "DELETE FROM user WHERE user_ip = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){

            pstmt.setString(1, user.getId());
            int rows = pstmt.executeUpdate();
            return rows > 0;

        }catch (SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    //获取
    public List<User> getAllUser(){
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM user";

        try(Connection conn = DatabaseConnection.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)){

            while(rs.next()){
                User  user = new User();
                user.setId(rs.getString("user_ip"));
                user.setPassword(rs.getString("user_password"));
                users.add(user);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return users;
    }
    public User getUserByIP(String ip){
        User user = new User();
        String sql = "SELECT * FROM user WHERE user_ip = ?";
        try(Connection conn = DatabaseConnection.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            pstmt.setString(1, ip);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()){

                user.setId(rs.getString("user_ip"));
                user.setPassword(rs.getString("user_password"));
                return user;
            }else{
                System.out.println("没有" + ip);
                return null;
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
    }
}
